﻿using Marmat.Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Marmat.Template.WinForm
{

    public class SelectionRecordForm : SelectionRecordForm<System.Data.DataRow, System.Data.DataTable>, ISelectionRecordForm
    {
    }
    public class SelectionRecordMultiForm : SelectionRecordMultiForm<System.Data.DataRow, System.Data.DataTable>, ISelectionRecordMultiForm
    {
    }

    /// <summary>
    ///  单选窗体-基于表(分页)
    /// </summary>
    public class SelectionRecordPaginationForm : SelectionRecordPaginationForm<System.Data.DataRow, System.Data.DataTable>, ISelectionRecordPaginationForm
    {
    }

    /// <summary>
    ///  多选窗体- 基于Table-分页
    /// </summary>
    public class SelectionRecordMultiPaginationForm : SelectionRecordMultiPaginationForm<System.Data.DataRow, System.Data.DataTable>, ISelectionRecordMultiPaginationForm
    {
    }

    /// <summary>
    ///  单选窗体-基于表(分页)
    /// </summary>
    public class SelectionRecordPaginationForm<Entity> : SelectionRecordPaginationForm<Entity, IEnumerable<Entity>>, ISelectionRecordPaginationForm<Entity>
    {
    }

    /// <summary>
    /// 多选窗体-基于实体--分页
    /// </summary>
    /// <typeparam name="Entity"></typeparam>
    public class SelectionRecordMultiPaginationForm<Entity> : SelectionRecordMultiPaginationForm<Entity, IEnumerable<Entity>>, ISelectionRecordMultiPaginationForm<Entity>
    {
        public void ColumnBuilderCommand(Expression<Func<Entity, object>> expression)
        {
            List<string> columns = new List<string>();
            if (expression.Body is System.Linq.Expressions.UnaryExpression unary)
            {
                if (unary.Operand is System.Linq.Expressions.MemberExpression member)
                    columns.Add(member.Member.Name);
            }
            else if (expression.Body is System.Linq.Expressions.MemberExpression member)
                columns.Add(member.Member.Name);
            else if (expression.Body is System.Linq.Expressions.NewExpression newExpression)
            {
                foreach (var item in newExpression.Arguments)
                {
                    if (item is System.Linq.Expressions.MemberExpression item_member)
                        columns.Add(item_member.Member.Name);
                }
            }

            void columnBuilder(SelectionRecordColumn column)
            {
                column.Cancel = !columns.Contains(column.ColumnName);
                column.Index = Array.IndexOf(columns.ToArray(), column.ColumnName);
            }
            this.ColumnBuilder = columnBuilder;
        }
    }

    public class SelectionRecordMultiForm<Entity> : SelectionRecordMultiForm<Entity, IEnumerable<Entity>>, ISelectionRecordMultiForm<Entity>
    {
        public void ColumnBuilderCommand(Expression<Func<Entity, object>> expression)
        {
            List<string> columns = new List<string>();
            if (expression.Body is System.Linq.Expressions.UnaryExpression unary)
            {
                if (unary.Operand is System.Linq.Expressions.MemberExpression member)
                    columns.Add(member.Member.Name);
            }
            else if (expression.Body is System.Linq.Expressions.MemberExpression member)
                columns.Add(member.Member.Name);
            else if (expression.Body is System.Linq.Expressions.NewExpression newExpression)
            {
                foreach (var item in newExpression.Arguments)
                {
                    if (item is System.Linq.Expressions.MemberExpression item_member)
                        columns.Add(item_member.Member.Name);
                }
            }

            void columnBuilder(SelectionRecordColumn column)
            {
                column.Cancel = !columns.Contains(column.ColumnName);
                column.Index = Array.IndexOf(columns.ToArray(), column.ColumnName);
            }
            this.ColumnBuilder = columnBuilder;
        }
    }

    /// <summary>
    /// 单选窗体-分页
    /// </summary>
    /// <typeparam name="Entity"></typeparam>
    public class SelectionRecordForm<Entity> : SelectionRecordForm<Entity, IEnumerable<Entity>>, ISelectionRecordForm<Entity>
    {
        public void ColumnBuilderCommand(Expression<Func<Entity, object>> expression)
        {
            List<string> columns = new List<string>();
            if (expression.Body is System.Linq.Expressions.UnaryExpression unary)
            {
                if (unary.Operand is System.Linq.Expressions.MemberExpression member)
                    columns.Add(member.Member.Name);
            }
            else if (expression.Body is System.Linq.Expressions.MemberExpression member)
                columns.Add(member.Member.Name);
            else if (expression.Body is System.Linq.Expressions.NewExpression newExpression)
            {
                foreach (var item in newExpression.Arguments)
                {
                    if (item is System.Linq.Expressions.MemberExpression item_member)
                        columns.Add(item_member.Member.Name);
                }
            }

            void columnBuilder(SelectionRecordColumn column)
            {
                column.Cancel = !columns.Contains(column.ColumnName);
                column.Index = Array.IndexOf(columns.ToArray(), column.ColumnName);
            }
            this.ColumnBuilder = columnBuilder;
        }
    }

}
